package com.syqc.choose.teamcar

import androidx.lifecycle.MutableLiveData
import autodispose2.autoDispose
import com.syqc.comlib.auto.AutoDisposeViewModel
import com.syqc.comlib.utils.AppContext
import com.syqc.comlib.utils.CacheMMKV
import com.syqc.entity.TeamsAndCars
import com.syqc.monitor.R
import com.syqc.net.ServiceHelper
import kotlin.concurrent.thread

/**
 *@author joker
 *@time   2024/5/31 11:17
 *@desc
 */
class CarsVM : AutoDisposeViewModel() {
    private val teamIds = arrayListOf<String>()
    private val teamNames = arrayListOf<String>()
    private var pid = "0"
    var isVideo = false

    private val rootName by lazy { AppContext.getDescById(R.string.desc_choose_root) }
    val carsLive = MutableLiveData<ArrayList<TeamsAndCars>>()
    val nameLive = MutableLiveData<String>()

    val changeTeamLive = MutableLiveData<Int>()

    /**
     * Get teams cars  获取车队和车辆信息
     *
     */
    fun getTeamsCars() {
        toastLoading(true)
        ServiceHelper.getRepository().getTeamsAndCars().autoDispose(this).subscribe({
            thread {
                TeamCarsUtil.handlerTeamCars(it, isVideo)
                carsLive.postValue(TeamCarsUtil.getTeamCars(pid))
                toastLoading(false)
            }
        }, error)
    }

    /**
     * Get team car 获取车队
     *
     * @param teamId
     * @param teamName
     */
    fun getTeamCar(teamId: String, teamName: String) {
        pid = teamId
        if (teamId == "0") {
            reset()
        } else {
            teamIds.add(teamId)
            teamNames.add(teamName)
            nameLive.postValue(teamName)
        }
        carsLive.postValue(TeamCarsUtil.getTeamCars(teamId))
    }

    /**
     * Reset  重置
     *
     */
    private fun reset() {
        teamIds.clear()
        teamNames.clear()
        teamIds.add("0")
        teamNames.add(rootName)
        nameLive.postValue(rootName)
    }

    /**
     * Get previous 上一辆
     *
     */
    fun getPrevious() {
        if (teamIds.size <= 1) {
            toast(R.string.desc_root_team)
            return
        }
        if (pid == "0") {
            getTeamCar("0", "")
            return
        }
        teamIds.removeAt(teamIds.lastIndex)
        teamNames.removeAt(teamNames.lastIndex)
        nameLive.postValue(teamNames.last())
        pid = teamIds.last()
        carsLive.postValue(TeamCarsUtil.getTeamCars(pid))
    }

    /**
     * Get root team  获取根车队
     *
     */
    fun getRootTeam() {
        if (teamIds.size <= 0 || pid == "0") {
            toast(R.string.desc_root_team)
            return
        }
        pid = "0"
        reset()
        carsLive.postValue(TeamCarsUtil.getTeamCars(pid))
    }

    /**
     * 保存最后选中的数据
     *
     */
    fun saveData() {
        var ids = ""
        teamIds.forEach {
            ids = "$ids,$it"
        }
        var nameS = ""
        teamNames.forEach {
            nameS = "$nameS,$it"
        }
        CacheMMKV.encode(if (isVideo) "ChooseVideoCarIds" else "ChooseCarIds", ids.substring(1))
        CacheMMKV.encode(
            if (isVideo) "ChooseVideoCarNames" else "ChooseCarNames",
            nameS.substring(1)
        )
    }

    /**
     * Init team  车队初始化
     *
     */
    fun initTeam() {
        val ids = CacheMMKV.decodeString(if (isVideo) "ChooseVideoCarIds" else "ChooseCarIds")
        val nameS = CacheMMKV.decodeString(if (isVideo) "ChooseVideoCarNames" else "ChooseCarNames")
        if (ids.isEmpty()) {
            teamIds.add("0")
            teamNames.add(rootName)
        } else {
            teamIds.clear()
            teamIds.addAll(ids.split(","))
            teamNames.clear()
            teamNames.addAll(nameS.split(","))
        }
        pid = teamIds[teamIds.lastIndex]
        nameLive.postValue(teamNames[teamNames.lastIndex])
    }

    /**
     * Change team  切换车队
     *
     * @param carId
     */
    fun changeTeam(carId: String) {
        ServiceHelper.getRepository().canChangeTeam(carId).autoDispose(this).subscribe({
             if (it.moditeam){
                 changeTeamLive.postValue(1)
             }else{
                 changeTeamLive.postValue(0)
             }
        }, {
            changeTeamLive.postValue(-1)
        })
    }

}